/*
 * Background Flickr: Copyright (C) 2009  Craig McNicholas
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.ddw.app.bflickr.data;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.ddw.app.bflickr.logging.Log;
import com.ddw.app.bflickr.logging.LogFactory;

/**
 * Helper class to handle upgrading and creating of a new database object
 * @author craig
 */
public class DatabaseHelper extends SQLiteOpenHelper {
	
	private Log log = LogFactory.createInstance(DatabaseHelper.class);
	private String databaseName;
	private int databaseVersion = 1;
	private String createStatement;
	private String destroyStatement;

	/**
	 * Constructs a new database helper instance to help creating and upgrading
	 * database instances.
	 * @param context the applications context
	 * @param databaseName the databases name
	 * @param databaseVersion the databases version number
	 * @param createStatement the SQL statement used to create the databases tables
	 * @param destroyStatement the SQL statement used to destroy the databases tables
	 */
	public DatabaseHelper(Context context, String databaseName, int databaseVersion, String createStatement, String destroyStatement) {
		super(context, databaseName, null, databaseVersion);
		this.databaseName = databaseName;
		this.databaseVersion = databaseVersion;
		this.createStatement = createStatement;
		this.destroyStatement = destroyStatement;
	}
	
	/**
	 * Getter of the database name
	 * @return the database name
	 */
	public String getDatabaseName() {
		return this.databaseName;
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		log.debug("Creating new '" + this.databaseName + "' database version " + this.databaseVersion);
		db.execSQL(this.createStatement);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		log.debug("Upgrading database '" + this.databaseName + "' from version " + oldVersion + " to " + newVersion);
		db.execSQL(this.destroyStatement);
		onCreate(db);
	}
	
}
